package require Tk
package require csv
package require struct::matrix
source "dateentry.tcl"
 
 
wm title . "Data Analysis Tool"
 
#initial frame
grid [ttk::frame .f -padding "3 3 12 12" -width 800 -height 600] -column 0 -row 0 -sticky nwes
grid columnconfigure . 0 -weight 1; grid rowconfigure . 0 -weight 1
 
#separate frame into 3 parts; left, right, bottom
grid [ttk::labelframe .f.left -text "Control"] -column 0 -row 0 -sticky nwes
grid [ttk::labelframe .f.right -text "Report" ] -column 1 -row 0 -sticky nwes
grid [ttk::labelframe .f.bottom -text "Graph" ] - - -sticky nwes
 
#control on the left, report on the right, graph at the bottom
grid [ttk::frame .f.left.control -padding "10 10 10 10" -width 400 -height 300] -column 0 -row 0 -sticky nwes
grid [ttk::frame .f.right.report -padding "10 10 10 10" -width 400 -height 300] -column 1 -row 0 -sticky nwes
grid [ttk::frame .f.bottom.graph -padding "10 10 10 10" -width 800 -height 300] -column 0 -row 1 -sticky nwes
 
#initialize control
grid [ttk::frame .f.left.control.pair -padding "10 10 10 10" -width 400 -height 200] -column 0 -row 0 -sticky nwes
grid [ttk::frame .f.left.control.custom_period -padding "10 10 10 10" -width 400 -height 50] -column 0 -row 1 -sticky nwes
grid [ttk::frame .f.left.control.graph_style -padding "10 10 10 10" -width 400 -height 50] -column 0 -row 2 -sticky nwes
grid [ttk::frame .f.left.control.indicator -padding "10 10 10 10" -width 400 -height 100] -column 0 -row 4 -sticky nwes
 
set ::symbol "EURUSD"
 
grid [ttk::combobox .f.left.control.pair.symbol -textvariable ::symbol] -column 0 -row 0 -sticky w
	.f.left.control.pair.symbol configure -values [list EURUSD USDJPY GBPUSD USDCHF USDCAD AUDUSD SPX500 JPN225]

	
grid [ttk::button .f.left.control.pair.go -text "Go" -command Go] -column 1 -row 0 -sticky w
 
#initialize custom period
set ::global_start_date "2011.01.01"
set ::global_end_date "2011.03.31"
 
proc getDate { args } {
	update
}
 
set start_date_entry [widget::dateentry .f.left.control.custom_period.start_date -textvariable ::global_start_date \
	   -dateformat "%Y.%m.%d" -command [list getDate .f.left.control.custom_period.start_date]]
set end_date_entry [widget::dateentry .f.left.control.custom_period.end_date -textvariable ::global_end_date \
	   -dateformat "%Y.%m.%d" -command [list getDate .f.left.control.custom_period.end_date]]
 
getDate .f.left.control.custom_period.start_date 
getDate .f.left.control.custom_period.end_date 
 
grid [label .f.left.control.custom_period.startl -text "Start:"] -column 0 -row 0 -sticky w
grid [label .f.left.control.custom_period.endl -text "End:"] -column 2 -row 0 -sticky w
 
grid $start_date_entry -column 1 -row 0 -sticky w
grid $end_date_entry -column 3 -row 0 -sticky w
 
 
#intial graph type combobox
set ::graph_style_selected "Simple"
 
grid [label .f.left.control.graph_style.label -text "Graph Style:"] -column 0 -row 0 -sticky w
grid [ttk::combobox .f.left.control.graph_style.combobox -textvariable ::graph_style_selected] -column 1 -row 0 -sticky w
	.f.left.control.graph_style.combobox configure -values [list "" Simple "Candle Stick"]
 
 
#initial indicator
set ::indicator_selected "None"
 
grid [label .f.left.control.indicator.label -text "Overlay:"] -column 0 -row 0 -sticky w
 
grid [ttk::combobox .f.left.control.indicator.combobox -textvariable ::indicator_selected] -column 1 -row 0 -sticky w
	.f.left.control.indicator.combobox configure -values [list "" SMA EMA ZigZag "Bollinger Bands"]
 
#initialize report
ttk::treeview .f.right.report.tree -columns {date x open high low close volumn} -show headings \
	-yscroll ".f.right.report.vsb set" -xscroll ".f.right.report.hsb set"
 
ttk::scrollbar .f.right.report.vsb -orient vertical -command ".f.right.report.tree yview"
ttk::scrollbar .f.right.report.hsb -orient horizontal -command ".f.right.report.tree xview"
 
grid .f.right.report.tree .f.right.report.vsb -in .f.right.report -sticky nwes
grid .f.right.report.hsb -in .f.right.report -sticky nwes
 
grid .f.right.report.tree -in .f.right.report -sticky nwes
 
 
 
proc LoadCsv {} {
 
	upvar ::symbol local_symbol
 
	#load data from csv file
	struct::matrix m
	m add columns 7
	#set csvfile "USDJPY1440_short.csv"
	set folder_path "data/"
	set file_extension ".csv"
	set csvfile $folder_path$local_symbol$file_extension
 
	set f [open $csvfile]
	csv::read2matrix $f m , auto
	close $f
 
	.f.right.report.tree delete [.f.right.report.tree children {}]
 
	set font [ttk::style lookup [.f.right.report.tree cget -style] -font]
	foreach col {date x open high low close volumn} name {Date X Open High Low Close Volumn} {
		.f.right.report.tree heading $col -command [list SortBy .f.right.report.tree $col 0] -text $name
	}
	foreach col {date x open high low close volumn} len {75 50 50 50 50 50 50} {
		.f.right.report.tree column $col -width $len
	}
 
	#matrix, m -> list of lists, data_table
	set nrows [m rows]
	set data_table {}
	for {set i 0} {$i<$nrows} {incr i} {
		set row [m get row $i]
		set date [lindex $row 0]
		set x [lindex $row 1]
		set open [lindex $row 2]
		set high [lindex $row 3]
		set low [lindex $row 4]
		set close [lindex $row 5]
		set volumn [lindex $row 6]
		lappend data_table [list $date $x $open $high $low $close $volumn]
		.f.right.report.tree insert {} end -values [list $date $x $open $high $low $close $volumn]
	}
	m destroy
 
	return $data_table
}
 
#initialize graph
source "drawing.tcl"
 
set data_table [LoadCsv]
set filtered_table [FilterDataFromStartDateToEndDate $data_table $::global_start_date $::global_end_date]
 
proc Reflesh {} {
	upvar ::global_start_date local_start_date
	upvar ::global_end_date local_end_date
	upvar ::graph_style_selected local_graph_style
	global filtered_table
	set s [RefleshCanvas .f.bottom.graph.canvas $local_graph_style $filtered_table $::indicator_selected]
}
 
proc Go {} {
	upvar ::global_start_date local_start_date
	upvar ::global_end_date local_end_date
	upvar ::graph_style_selected local_graph_style
	global filtered_table	
	set local_data_table [LoadCsv]
	set filtered_table [FilterDataFromStartDateToEndDate $local_data_table $local_start_date $local_end_date]	
	set s [RefleshCanvas .f.bottom.graph.canvas $local_graph_style $filtered_table $::indicator_selected]
}
 
proc ShallowReflesh {canvas x y} {
	Reflesh
	ShowXY $canvas $x $y
}
 
grid [tk::canvas .f.bottom.graph.canvas -background white -width 800 -height 250] -column 0 -row 0 -sticky nwes
bind .f.bottom.graph.canvas <1> "ShallowReflesh .f.bottom.graph.canvas %x %y"
bind .f.bottom.graph.canvas <B1-Motion> "ShallowReflesh .f.bottom.graph.canvas %x %y"
Reflesh
 
 
proc ShowXY {canvas x y} {
	global filtered_table
 
	set width [$canvas cget -width]
	set height [$canvas cget -height]
	set border 40
	set graph_w [expr {$width - 2*$border}]	
	set y_orig [expr {$height - $border}]
	set x_orig $border
	set xmax [llength $filtered_table]		
	set x_scale [expr {$graph_w / $xmax}]
 
	set plotFont {Helvetica 12}
 
	set i [expr { [expr {$x - $x_orig}] / $x_scale}]
	set x_round [expr {$x_orig + $x_scale * $i}]
	set data_value [lindex $filtered_table $i]
	set date_value [lindex $data_value 0]	
	set y_value [lindex $data_value 2]	
 	set formattedDate [string map {. /} $date_value]
	
	set xy "date : $formattedDate\nclose: $y_value"
	$canvas create text [expr {$x_round+60}] 30 -text $xy -font $plotFont -fill black
	$canvas create line $x_round $y_orig $x_round $border -width 1 -fill blue
}